package com.example.seakdreamandroid.sleep;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.seakdreamandroid.FavoriteActivity;
import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.MusicStoryPlayerActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.bedtimeStory.BedtimeStory;
import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.sleep.util.MediaUtil;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerActivity;

import java.util.ArrayList;

public class NapActivity extends Activity {

    private TextView tvStart;
    private TextView tvMinute;
    private TextView tvNapScience,tvNapEfficient,tvNapTravelMode,tvNapComplete;
    private ImageButton imgBtnNap , likeList, story ;
    private ImageButton imgBtnReturnSleep;
    private RelativeLayout napRelativeLayout;
    private String tvMinuteText = "";
    private int millisInFuture;
    private ArrayList<String> minuteList = new ArrayList<>();
    CountDownTimer countDownTimer;
    private boolean isBrewing = false;
    private HorizontalScrollView horizontalScrollView;
    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nap);

        getViews();
        user = (User) getApplication();
        tvMinute.setText("请选择类型");

        initData();
        //注册监听器
        setListener();
    }

    private void getViews() {
        tvStart = findViewById(R.id.tv_start);
        tvMinute = findViewById(R.id.tv_minute);
        tvNapScience = findViewById(R.id.tv_napScience);
        tvNapEfficient = findViewById(R.id.tv_napEfficient);
        tvNapTravelMode = findViewById(R.id.tv_napTravelMode);
        tvNapComplete = findViewById(R.id.tv_napComplete);
        imgBtnReturnSleep = findViewById(R.id.img_btn_return_sleep);
        imgBtnNap = findViewById(R.id.imgBtn_nap);
        napRelativeLayout = findViewById(R.id.nap_relativeLayout);
        horizontalScrollView = findViewById(R.id.HorizontalScrollView);
        likeList = findViewById(R.id.likelist);
        story =findViewById(R.id.story);
    }

    private void setListener() {
        MyClickListener myClickListener = new MyClickListener();
//        tvNapCustomize.setOnClickListener(myClickListener);
        tvNapScience.setOnClickListener(myClickListener);
        tvNapEfficient.setOnClickListener(myClickListener);
        tvNapTravelMode.setOnClickListener(myClickListener);
        tvNapComplete.setOnClickListener(myClickListener);
        imgBtnReturnSleep.setOnClickListener(myClickListener);
        likeList.setOnClickListener(myClickListener);
        story.setOnClickListener(myClickListener);
    }


    private void initData() {
        minuteList.clear();
        for (int i = 1; i <= 90; i++) {
            if (i < 10) {
                minuteList.add(String.format("0%d分钟", i));
            } else {
                minuteList.add(String.format("%d分钟", i));
            }
        }
    }

    private class MyClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch(view.getId()) {
                case R.id.tv_napScience:
                    napRelativeLayout.setBackgroundColor(getResources().getColor(R.color.napScience));
                    millisInFuture = 10;
                    setTiming(millisInFuture);
                    break;
                case R.id.tv_napEfficient:
                    napRelativeLayout.setBackgroundColor(getResources().getColor(R.color.napEfficient));
                    millisInFuture = 24;
                    setTiming(millisInFuture);
                    break;
                case R.id.tv_napTravelMode:
                    napRelativeLayout.setBackgroundColor(getResources().getColor(R.color.napTravelMode));
                    millisInFuture = 40;
                    setTiming(millisInFuture);
                    break;
                case R.id.tv_napComplete:
                    napRelativeLayout.setBackgroundColor(getResources().getColor(R.color.napComplete));
                    millisInFuture = 90;
                    setTiming(millisInFuture);
                    break;
                case R.id.img_btn_return_sleep:
                    Intent intent = new Intent(NapActivity.this, MainActivity.class);
                    intent.putExtra("id",1);
                    startActivity(intent);
                    break;
                case R.id.likelist:
                    startActivity(new Intent(NapActivity.this,FavoriteActivity.class));
                    break;
                case R.id.story:
                    if (user.isMSPIsPlay()) {
                        Intent intent1 = new Intent();
                        intent1.putExtra("audioUrl", user.getAudioUrl());
                        intent1.putExtra("audioId", user.getAudioId());
                        intent1.putExtra("audioName", user.getAudioName());
                        intent1.putExtra("audioAuthor", user.getAudioAuthor());
                        intent1.setClass(NapActivity.this, MusicStoryPlayerActivity.class);
                        startActivity(intent1);
                    } else if(user.isWhiteNoiseIsPlay()){
                        Intent intent1 = new Intent(NapActivity.this, WhiteNoisePlayerActivity.class);

                        startActivity(intent1);
                    } else{
                        Intent intent1 = new Intent(NapActivity.this, MusicStoryPlayerActivity.class);
                        intent1.putExtra("audioUrl", "https://link.jscdn.cn/1drv/aHR0cHM6Ly8xZHJ2Lm1zL3UvcyFBcDBqdlhfVUliRW5nUnRuYlZ6WjRCNktHSEVTP2U9TnFzQTN3.mp3");
                        intent1.putExtra("audioId", "61");
                        intent1.putExtra("audioName", "海的女儿");
                        intent1.putExtra("audioAuthor", "精选童话");
                        startActivity(intent1);
                    }
                    break;
            }
        }
    }

    private void setTiming(int i) {
        tvMinute.setText(String.format("%d分钟", i));
        tvMinuteText = this.tvMinute.getText().toString();
        imgBtnNap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isBrewing) {
                    stopBrew(tvMinuteText);
                    horizontalScrollView.setVisibility(View.VISIBLE);
                } else {
                    horizontalScrollView.setVisibility(View.GONE);
                    countDownTimer = new CountDownTimer(i * 60 * 1000, 1000) {
                        @Override
                        public void onTick(long m) {
                            tvStart.setText("点击暂停");
                            if (tvMinute != null) {
                                tvMinute.setClickable(false);
                                tvMinute.setEnabled(false);
                                long minute = m / (1000 * 60); //单位分
                                long second = (m - minute * (1000 * 60)) / 1000;//单位秒
                                tvMinute.setText(String.format("%d" + ":" + "%d", minute, second));
                            }
                        }

                        @Override
                        public void onFinish() {
                           MediaUtil.playRing(NapActivity.this);
                            AlertDialog.Builder builder = new AlertDialog.Builder(NapActivity.this);
                            builder.setTitle("关闭闹钟")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            MediaUtil.stopRing();
                                            onCreate(null);
                                        }
                                    });
                            AlertDialog alertDialog = builder.create();
                            alertDialog.show();
                        }
                    };
                    countDownTimer.start();
                    isBrewing = true;
                }
            }
        });
    }
    public void stopBrew(String str) {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        isBrewing = false;
        tvMinute.setText(str);
        tvStart.setText("开始小憩");
    }

}